package com.fruit.audit.controller;


import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.audit.bean.TutorUser;
import com.fruit.audit.service.ITutorImageService;
import com.fruit.audit.service.ITutorUserService;
import com.fruit.tool.util.ResultBean;
import com.fruit.tool.util.ResultUtil;

@Controller
public class CommonController {
	private static Logger log = LoggerFactory.getLogger(CommonController.class);
	
	@Resource(name = "tutorUserService")
	private ITutorUserService tutorUserService;

	/**
	 * 用户登陆
	 * @param email 用户邮箱
	 * @param password 用户密码
	 * @return
	 */
	@RequestMapping("/login/{v}")
	@ResponseBody
	public ResultBean loginUser(@RequestParam(required = true) String phone, 
					@RequestParam(required = true) String password,
					@RequestParam(defaultValue = "") String deviceInfo,
					@PathVariable String v) {
		log.info("开始登陆phone=" + phone + ",password=" + password + ",v=" + v);
		
		try {
			TutorUser user = tutorUserService.loginUser(phone, password);
			
			return ResultUtil.returnResult(user, "登陆成功");
		}  catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.returnResult(e);
		}
	}
	
	
	/**
	 * 用户登出
	 * @param token 用户邮箱
	 * @return
	 */
	@RequestMapping("/logout/{v}")
	@ResponseBody
	public ResultBean logoutUser(
			@RequestParam(required = true) String token,
			@RequestParam(required = true) String deviceInfo,
			@PathVariable String v) {
		log.info("开始登出token=" + token + ",deviceInfo=" + deviceInfo + ",v=" + v);
		
		try {
			tutorUserService.loginOut(token);
			return ResultUtil.returnResult("", "登出成功");
		}  catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.returnResult(e);
		}
	}
	
//	/**
//	 * 打印图片
//	 */
//	@RequestMapping(value = "/draw/{v}")
//	public void DrawQRCode(String imgId, HttpServletResponse response) {
//		try {
//			String imgPath = tutorImageService.getImagePathById(imgId);
//			BufferedImage bufImg = ImageUtil.readImage(imgPath);
//			ImageIO.write(bufImg, "jpg", response.getOutputStream());
//		} catch (Exception e) {
//			log.error(e.getMessage(), e);
//		}
//	}
}
